<template>
  <div>
    <el-dialog
      v-model="visible"
      :title="$t('components.editName') + '/' + $t('product.status')"
      width="36%"
      class="dialog-update-user-info"
    >
      <el-form
        ref="dataFormRef"
        :model="dataForm"
        class="demo-form-inline form"
        @submit.prevent
      >
        <el-form-item
          :label="$t('users.name') + ':'"
          prop="nickName"
        >
          <el-input
            v-model="dataForm.nickName"
            :disabled="false"
            :placeholder="$t('users.name')"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item
          :label="$t('publics.status') + ':'"
          prop="status"
        >
          <el-radio-group v-model="dataForm.status">
            <el-radio :label="0">
              {{ $t('publics.disable') }}
            </el-radio>
            <el-radio :label="1">
              {{ $t('publics.normal') }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <div
            class="default-btn"
            @click="visible = false"
          >
            {{ $t('remindPop.cancel') }}
          </div>
          <div
            class="default-btn primary-btn"
            :loading="confirmLoad"
            @click="onConfirm"
          >
            {{ $t('remindPop.confirm') }}
          </div>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'
const emit = defineEmits(['refreshUserInfo'])

const page = ref({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const visible = ref(false)
const confirmLoad = ref(false)
const dataForm = ref({})
const dataFormRef = ref(null)
const init = data => {
  visible.value = true
  dataForm.value = JSON.parse(JSON.stringify(data))
}
const onConfirm = () => {
  if (!dataForm.value.nickName || !dataForm.value.nickName.trim()) {
    return ElMessage.error($t('users.noEmpty'))
  }
  dataFormRef.value.validate(valid => {
    if (valid) {
      confirmLoad.value = true
      http({
        url: http.adornUrl('/admin/user'),
        method: 'put',
        data: http.adornData({
          userId: dataForm.value.userId,
          nickName: dataForm.value.nickName,
          status: dataForm.value.status
        })
      }).then(() => {
        ElMessage({
          message: $t('publics.operation'),
          type: 'success',
          duration: 1500,
          onClose: () => {
            visible.value = false
            confirmLoad.value = false
            emit('refreshUserInfo', page.value)
          }
        })
      })
    }
  })
}
defineExpose({ init })
</script>
